Culled dead code, added comments, improved formatting.