File handling incorrect