Getting rid of recursion