fixed a couple of bugs, added stream interface, and did a big fat refactor