Fixed issues #20 #21 #16 and #22