add mouse support and some refactoring