Refactor common code and added trace logger