Reworked code to reduce number of unnecessary virtual function calls