Solution by Nuno Vieira.