Tidied up some logic errors