Lots of refactoring, some misc bug fixes, working phase 2 product.