Fix "list index out of range" error in admin panel.
Fix code duplication.