add error handling for all memory management