fixes for issues #4, 140, 168, 169, 170, 171