fixes #856
tidy up
reordered if/else statements