Implement the enumeration functions

I ought to have used some macros in these, would have made everything a
lot nicer... not sure why it didn't occur to me.