Fixed bugs reported by jshint.
Reduced some complexity.