Some fixes for issue #14

I hope this is a good start to fix some issues pointed out by @nikic in issue #14.