Partially refactored client-side javascript...NOT DONE YET