adding some comments and removed unnecessary loops