Clean up and optimize code