Refactored JS code

Refactored the JS code and made it so that drawGraph() gets called after
updating the graph.