Refactored mouse logic.