fixed tests + some minor refactoring