First version of the solution. To be revised