Big refactor, prep to create web api interface

build is broken but hopefully the refactor will solve the 'misery with
the Chain object