Refactor library, fix input functions

Moved a lot of code out of main.m, mostly into lib.m.
This will help facilitiate some future refactors.

Updated case 3:5 to conform to the new variable conventions
for current_pv*_maxes.  The last patch was broken and would crash as a result.