Improve the apply and unapply function