Tidied up, added logging and removed some old code