fixed jshint errors. Added guards. Some more cleanup.