Reorganized code, added state machine, implemented seek support.