Refactor javascript/client-side