Pulled logging out into a func, tidied bits up, added data cache