working version slow
numba optimized