Fixed bug due to not checking for NULL