factored out multiplexer and renamed output to demultiplexer; tests; some more logging