move flush inside the callback