Added E, and restructured A-D to use nearest neighbor function