some refactoring but still WIP