refactor the code