Fixed numerous codegen bugs. Fuck off by 1 errors.