refactor code to support in/out streams