Some refactoring of the code