Several bug fixes and changes to build process