Moved the meat of the code to core, pending breaking up