extract out the key logic