Add proper error handling and save function