Fix #20. Added changes from François and fixed some bugs.