Refacor code and make jslint acceptable