A bit of refactoring. Decided not to document it using Doxygen yet