Tidy up of javascript code for filtering