Initial draft of cleaned-up code