Code Refactoring and Cleanup