Fixed #7 + some refactoring