refactoring js a little