Some refactoring and cleanup of controller detection.