Added error handling for NoneType