Reworked some of the logic