optimized code slightly