Tests refactoring and minor fixes