Simplify approach and fix several bugs to get this working with elli's master branch.