add logging and modify the code