Move shared code to base class and optimize it to make as less memory copies as possible