changed a load of crap, split code up into modules