Consolidated and cleaned up code, among other things