Massive rewrite. Semi working at the end