More improvements, tests and bugfixes