use some cython functions for speed