added if statement to avoid nil nilclass error