half part way through refactoring this mess
got it working again after a major refactor