first cythonized version, about 12% faster