Cherry pick first part of 2.6 fix
Improve the previous 2.6 fix: use bytes rather, than string.
Also, optimize for better speed.