Fix out of bounds bug when layeringg