Clean up and make more resilient