The following patch with updates for trunk. Works much better in trunk.