edited Javascript code for clarity.