refactoring and small javascript improvements