Moved some code around so that it is a bit more clear what can be subclassed.