cleanup and error handeling