Fix substring bugs