More robust version of my code.