cleaned up and refactor the codes