add handlers and some refactoring