Fix possible crashes

This fixes issue #29