Do proper session teardown and potential rollback

Fixes #33.