Eliminated some forloop inefficiencies