Major refactor. Almost working