refactoring code to make it cleaner