add cert_file option, fix some issues with the tests, refactor a bit