refactor of js code