Add assertions and fix a few bugs