added PrologEngine class, various fixes and improvements