Updated file handling
Final working version. Commented and cleaned up code too