Fixing first and last element bug for layers.