improved buffer overflow handling