finally properly nested error checking and resource release
proper types to eliminate warnings