Complete refactor. More modular and tested