Clean up mouse event handling function