Miscellaneous fixes plus numerous tests