Refactoring, and various fixes related to categories management