Cleaned up code, changed some structure, improving paging logic.