solve algorithm

still a bug (returning nil)