A hopeless attempt to clean up the javascript code