Moved first to prevent possible memory leaks