Re-worked some of the logic