Fix issues #299, #306, #311