Fixed clear button behaviour, tidied up some javascript