Replace map/flatten with flat_map

flat_map is faster and uses less object allocations.
See my bench setup & results: https://gist.github.com/wojtekmach/925a780b844dc1d7114a

Thanks to @sferik for a presentation on speeding up ruby:
https://twitter.com/sferik/status/511435110906736640