Fixed tests + refactoring