Fixed if statements breaking things.
Fixed reference to dt to be variable rather than constant.