replaced memory-streams with own implementation