Refactored, and added new logic.