made some performance tweaks to reduce runtime by 60%