fixed a bug with programme avoiding I think, but still not tested fully, and lots of logging in there