Broke logic into separate functions