re-orged code flow