Better memory management

There were a few return values of functions that allocates memory that
weren't being checked. Therefore, if an ret_nomem error happened, for
instance, it wasn't propagated properly.