Reverting @789207d30a with the exception of the actual correction to the logic