Various bug fixes (I swear this is the last one)