Redo the implementation. still a long way to go, but it seems to work