wow this function is long. Ideally would be nice if this broken up into parts but most likely to late now