Functions to be moved to cython later