Set type directly upon file creation.

This relies on other commits from a different pull request, #194, included here for simplicity.

You can either review the first pull request, then this one, or this one only.