Backport changes to gpu.cpp - note - currently broken in some ways