Switched from recursive to loop because StackOverFlowError.