#17 Improved flag code considerably