js refactor/debugging