moved triangle code to function