Massive restructuring. Not complete yet but functional with some bugs