Fixes security issues and improves save behaviour. Props @azaozz.