Javascript code cleaning and fixes