Refactoring: code cleanup and formatting