Removing java code and changing map + flatten -> flatMap