Some cleanup and optimization. Minified version is now 1004bytes.