Added comments and removed some redundant code, pls check if the comments are accurate