there is not much code shared between insert, prepend and append. You should create a common function to get the fragment (which is shared between the functions), and then move the specific code in prepend and append instead of having this spaghetti code in insert.