refactoring and modularization; add mouse control and other useful functions