VID-1788 Put logic into functions