optimized fastaply code and updated tests