Fixes array merging logic and adds tests.