added more robust nil handling