Merge in latest WIP

Merge in latest code for @chuong. some is missing tests, but most works.