Fix issues #7 #2 and  part of #4