Optimized version. Faster than CPU