Lot of work on cleaning up the implementation