JS refactoring & cleanup