Refactored the codes