First complete solution - I don't know if the result is right and it is also not optimized