Issue #29 work. Improved error handling.

Now throws exceptions and frees memory.