added summary, code formatted and minimally cleaned