Cleanup and refactoring