Rearrange and clean up. Fixes #188