imporved solution to P1703