Small optimizations, code refactoring, jshint fixes
Merge pull request #436 from kyuri/upstream

Small optimizations, code refactoring, jshint fixes