This and the remaining file is begging for some OOP IMHO.