PR to fix #24, #29 and #30

Here's a revised PR