Moved logic to func