Corrected flag functions and added unit tests.