Fixed up logic to be (hopefully!) less silly.