Finally got a working implementation of everything.