refactoring. divided to 'stream handling layer' and 'frame handling layer'.