improving the code once more for more amenable porting to CUDA