Tidied up the code.
Now using functions for registering fences.