changed to use StreamUtil. Untested. May contain bugs