Finally working

Lots of changes but now it seems to work ok on Win7/R2013a.