fix exits on error

Clean up the last of the cases where the library exit(1)'s on error and
return NULL or -1 in those cases. Also add some more function comments.