fixed loop edge conditions