Added part to determine beginning and end