modified papply to be more dynamic