You can use any better class to replace Pair. However, I think we do not need to reimplement `flatMap` again here.