modify loop condition from issue #7