Iteration logic and plot function.