Make low-level API return the same values as C implementation and other
changes to minimize difference from C code.

IMPORTANT! This change makes low-level API incompatible!