Fix issues with gather() algorithm

This fixes some issues with the gather algorithm and also
adds another test for it.