Added the majority of the code to make this thing functional