Code refactoring and formatting