corrected flag calculation