More bug fixes introduced in rewrite. Also lots of TODO marks