Added bugfixes and tests