your idle problem could be caused by a dirty Air Flow Meter (AFM).
try disconnecting the lead from it and see if the idle levels out.
if it does, you can try cleaning the AFM using some carb cleaner.
failing that it could also be the Idle Control Valve (ICV)-this can be disconected and cleaned too (careful tho

its Ԛ£50+ for a new one!)
one other option could also be an air leak somewhere in the air intake piping, so check that carefully.
Hope this helps
see pic below for locations of the ICV and AFM on the m40 1.8i
