Fix bugs and warnings

Fixes for a few bugs etc I've found while reviewing more code.