js refactoring & fixes