Use cpdef to avoid repeated python overhead