refactored prolog engine