Cleaning up js code for filtering