updated code to more object-oriented version. Still very much a work in progress