Some improved mouse handling code.