optimize the code to improve performance

optimize the code to improve performance