[WIP] Fix cuda

Various fixes and improvements for CUDA backend.