added ternary, some reformatting and fixes