first working version of demultiplexing