Improve exception on empty/used stream. Clean up some code a bit