Bug fix and Comments

Fixed several non-terminating while loops, removed several unused elseif
statements, and added comments for functions.