Fix FTBFS and installation errors

Please refer to the commit messages for details.