Bug corrections and some improvements

Tested with a demo app