Added permissions, and more error checking. Crude, but workable version.