Review comments, add more tests and fix a few edge cases