Fixed Flag issues and reworked some code