Fixes from review.
rewrite, include edge cases