added explanation and cleaned the solution a bit