added tests and closed outstream,small refactoring