Improve error handling

The error return value was ignored in several cases.