Added logging and cleaned up some things