Various bug and style fixes