Refactoring to make code manageable