Removed unnessary comments and codes and handled more edge cases