Made minor changes to formatword function