Fix some logical bugs.