Code from zlib.net project