Bug #2 fixed. Program now terminates reliably.