improved POJ 1401