refactored the logic and included subtraction