added bounds testing and made it stop looping