Fixed some exception handling. Added clause to defeat infinite loops.