Refactored Entire Code