optimized code for remove and insert functions

added ability to remove or insert from head or tail depending on which is closer to specified position