Corrected ugly function