Changed some if's to elif's.
Reworked some of the while-true check loops so that the internal logic is
a bit easier to understand.