Add flatMap, make functions serializable.