I think some object oriented code would certainly help a bit here.