Should be >=. Also the entire check should be moved to the start of the loop.