abstract logic into function