Refactor clientside javascript