Working code. Needs fiddling.