Completion of algorithm, having trouble with a jQuery table bug