Added hasClass and small refactoring and optimisations