Bug fixes. 

Fixes for #97, #87 and other minor fixes.