modified: got rid of instance variables and recursion.