Added a more robust term parser for Prolog terms