add checks for possible buffer overrun