Bug819

Fix bug #819 and some other issues.