Modified Haug's code by pulling the main body of his numerical procedure into two loops.