Functional version of the code

This code is functional, though not without bugs. From class 11/14.