Some refactoring, and added concept of handlers