Fixed the bug which we have spent the past few hours looking for.

Do *NOT* look at the diffs. They would embarass me.