Implemented logging and cleanup.