Clean up code and optimize