Optimized version of code