Refactor to flask