added some edge checking