improved the optimized version