Refactoring to fix bugs in the WebGL backend.