Restructure the hack to be more correct, and add support for double texture coordinates. Fixes #2314.
Also address various coding style issues.