improve speed by removing recursive call