Big cleanup to avoid a singleton and simplify the code for unit testing