split javascript function into separate files, plus minor fixes