Added/changed some things so that the code supports diagonal neighbors as well.