Two things I see missing from your code: A current substitution and a unification function.
This paper might also be helpful to read: http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.65....