Code cleanup

Cleanup code such that it passes jslint. Also, fixed a few small bugs here and there.