Fix loopForever

Check for exceptions, and add a method to break out of the loop. Also fix some memory leaks.