reduce outofmemory errors; add unit tests