Changed file open mode and fixed opening bug