lots of conditional logic for flags and params