refactored some things for RAII