Matches reference implementation