Port of the python version.