Some refactoring and implementation of the singleton pattern