cleanup to use early return style and separate function and tweak comments