Semi-working. Output not quite right