modify according to comments and add unit test