Added comments and finalized the implementation