Code refactoring and cleanup.