change async for when and refactor