fixed bug: check if fp != null before closing fp