Some refactoring and code cleanup, mainly to avoid cluttering the global
namespace.