Fixes #16, #20 and #12.