Overhauled data structure
Also cleaned up all the code.