Pulling out logic into corresponding namespaces. Introducing error handling.
Cleaning up stream creation logic from buffered image