fix pagination

This fixes the pagination not working at all. After the refactoring the
modification of the SQL query was lost which resulted in pagination not working.