Fix various warnings

See the comments inside each commit. The last commit is the most important one.