add scoring function; fix memory leak; refactorings