Fix error handling for prepare, result and query

Closes #2