Refactor the codes and Improve performance