Reorganized and renamed bunch of code