Algorithm improovemnts