Miscellaneous bug fixes. Added miscellaneous tests.