Pagination working and code refactored