Tidy up the logic