reorganize code, event dispatch