Refactored mouse handling.