changed implementation to use stream instead of fs.write