Major surgery: moved everything into class files and added some OO