took some refactoring but now everything is working