Fix unit test

Multiples bug fix from a massive refactor and fix unit tests