dynamic casts and some code clean up in category.cpp