Tidied up a lot of code and made channel calls thread safe