Added writing to PB from C, bugfixes.
Rewriting sweeping to C.