Improve code for advanced optimization