Refactor client side javascript code