cleanup and add error handling