added error handling and finalized