Clean up and comment code, also addresses some edge cases
Clean up and comment code, also addresses some edge cases

Peilun-Li/dynet,Abasyoni/dynet,Peilun-Li/dynet,Peilun-Li/dynet,Abasyoni/dynet,Abasyoni/dynet