Finally, bugs in new subroutines frequently have nothing to do with the routine itself, but with the calling of the routine through the subroutine interface. To reduce the complexity of such debugging, you may which to first write the subroutine as a main program to get it up and running and debugged. Then, once you know for sure that the code works, cut the relevant parts from the main program and paste them to the subroutine and make sure that you produce exactly the same results. In the first phase, you can be sure that there are no subroutine interface bugs and, in the second phase, you can be sure that there are no actual bugs in the routine. Reducing the possible phase space of bugs always speeds the debugging process, often exponentially.