sorting and pagination; need some code cleanup