Refactoring, bug fixes and better accuracy