Comments added where needed (mostly)
Several bug fixes