add logger and optimize code