fix resource issue and error handling