Fixed possible crash and small cleanup