Caching solve function

Debugged, still lacking unit tests