Completed logic for lab3.  TODO: clean up comments etc