Refactoring code based on functionalities