Refactor event/flush handling to use a thread pool instead of threads directly