fixed checking for buffer overflow