replaced dojo forEach loops with while loops in key spots, added an objectIdHash for quicker lookup, addresses issue #5
fixed some formatting