refactoring and cleaning up the code some