Add tests for new functionality and fix bugs they uncovered