Add more bugfixes and tests