rearrange the code so that it hopefully works better and fixes labels