Fix bug on length of result written to buffer