Performance improvements and add copyToStream method with test.