refactored and removed global vars, fdopen-open file test updated. Tests should fail due to bug.