fix miscellaneous edge cases; add more tests