many changes. now works, but not quite perfect.