Reorganized things and fixed some NameErrors