First refactor of the code