fixes calculation of required buffer length
fixes validating issues