Merge sugestion from refmodel branch to master

Reference model is running without fuss. Also the code didn't become so ugly or compicated as I imagined. I think it should be merged to master. I will wait for your agreement :-)