third version, some typos where corrected in comments