Bug-fixes courtesy of @matter123

Bug fixes in the c++ codebase courtesy of @matter123
A bug was fixed in build.py as well.