performance tweaks according to profiler. still greater improvement is needed.