Fixed error flows, return -ENOMEM when possible.