Fix quality flows and do some refactoring