Add tests, fix minor bugs