Cleaned up code. Added time function.  Renamed some variables to be clearer. Added more docs.