code refactored by C4ptainCrunch