fixing to make tail recursive and less wrong