Functions and cleanup.