more bug fixes, mostly on state.buffer.length, as well as local functions