break flag parsing into its own method, add tests