adding comments and adding tests. Also a bug fix