Tidying and fixing a JS error