Refactor onflush