Added rank detection in sparse implementation