Consolidated some loops to improve efficiency