polished code, added comments, minor updates