clean up code, smarter bitstream parsing