Update document enumeation loop boundary checks

- improve performance by moving boundary check from inside to outside of loop
- minor modification to view category handling