change rebuild to be robust (but couple of lines should be cleaned up still)