Working implementation of scoring function