Finished the append and flatten functions