Adapt for multiple samples

This implementation uses a lot of memory, potentially.