refactoring JS - abstracting functions and removing unused definietions