improved the flag check logic, added comment block