Fix crashes and code compilation

Here's a fix to the pull request #30 I made a few days ago.
Sorry for the bad initial pull request.