reformatted to reduce code duplication; added prev/next by category