refactoring mouse code