simplify the code by using array_map and join

The testsuite still passes after my changes and the code gets much
easier. Personally, I wouldn’t even put these functions into a module in
the first place, but YMMV :).