use raii wrapper to ensure everything gets released