refactored code to make it cleaner.