added cleaned-up prolog version