Code refactoring and reformatting