refactor the code to imporve the performance