Revert "Revert "lots of bug fixes and enhancements""

This reverts commit 249d340455e20e3c338dfccda66e8d38b896b085.