optimise cython code, added test cases, carrying on