Fix handling of quoted ] characters and garbage-after-close-quote logic