Improve logging and move some stuff around