Refactor the codes