fixes bug with filtering paginated collection, cleans up the code