A huge amount of refactoring was done here. This code looks/works better and has been reduced by over 300 lines.