Combined most of append and prepend into a single function.