Add the performance improvement code for issue #261 and #224