refactor the codes