Solution for #18
Full solution for #18