Comments added, with minor changes to code