Deduplicated code, eliminated manual array<->collection transfers.

// Unrelated to this pull-request:
@mateiz, I also took liberty to uncomment that line here: b21cbe688fdf5136b7d2569b1edc19fea117a8cc. Let me know if you consider it worth separate pull-request or should we wait?

UPD: Now it looks even more clearly duplicated for me, maybe I should try to generify it even more?