code refactored, thanks to C4ptainCrunch_