Refactor: fixed js according to jshint and jslint

Fixed messy whitespace, unused variables, moved functions and combining
statements.