fixed edge issues and improved efficiency of neighbor checking