fixed some issues with my translation of the old codebase. Flag calculation needs much better test coverage.