better memory overflow handling