Extract condition-check from loop, fix parenthesis