code improve thanks to griffpatch | ScratchStats