adds bounds checking so program does not run forever