Unit tests for rating calculation and some refactoring