Various fixes which should now resolve the nginx 'too many files open' error message, committing ready for testing.