Fix bug #747 and fix some typos in the code too