change function's position and add error message for file open