Advanced tests
Created by: grigorig
This improves the test suite, fixes a bunch of things found by the new tests and includes various cleanups.
The tests should ideally check that compatibility doesn't regress, but I'd like to see some additional verification with actual devices before merging it, if possible.