added checking for buffer size