Some cleanup, ahead of API work

rebased on #213