Common code for handling files