Fixed error caused by NoneType object access