Cherry-picked Stephen's changes - closes #22

- Had to resolve some conflicts manually