Added commentary and cleaned code