Refactored cuda code