Cleanup logic and formatting