Change flag handling to a much simpler format

Change the was flag handling works to a much much more simpler and efficient
way of working.