Fix several bugs found by PyCharm.