Refine the javascript code