Added factory + some refactoring