Fix some minor logic issues to handle all cases.