flatMap and refactor