Massive refactor and fixes, final commit