First draft of feature complete code. Still needs a lot more testing.