refactor mouse event function