Fixed flag inconsistencies and functionality issues; added a test