Added code so computation really stops when upper limit is reached