Attempt at refactor, but I broke some stuff, to resume later