Refactored the extract-categories function. A more "functional" implementation, and I believe easier to read.