Updated to work with Safari (needs re-testing on Chrome and Firefox)