Refined recursive function

Increased efficiency a bit with the neighbor traversal and if-else stuff
thingies
and removed some extraneous thingies

Later goal would be to reduce the repetitive code