Refactoring the code for better structure