refactored the code some more