One problem you could be having, is you're calling your sub routines without initializing firstRecord. As a result it contains random data, which could be causing your routine to crash. Try something simple like int firstRecord = 0;.

If you still have a problem submit some test data which will highlight the problem.