Working function, still testing boundary condition threshold=5000