Fix crashes

This will close bug #19 and another one introduced with the last commit.