Fix substring extraction