some refactoring and commenting on the flask side