Why didn't you just change it to `while(ptetry <= b*pagesperblock)` ?