optimized code
works!