Works on cpu
Working gpu version