Do you have some sample input? PJ Plauger wrote a great set of essays called Programming on Purpose and one of them said the structure of the code often looks like the structure of the input. It's hard to guess what the program will look like without knowing what the data looks like. Of course, another essay said the structure of the program often looks like the structure of the OUTPUT. Heh heh heh.
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi