moved logic into functions