improve error handling when out of memory