changed code for filtering $records