Updated version to write directly to graphics device