Slight change to alignment function that causes endless loop sometimes