Refactor event system to use onFlush