Legal flaw overturns 30-year jail sentence in rape case