Added additional buffer length check