Added logging and cleaned up the code