Modified to make cancel work