Rewrote large portions of code to hopefully simplify things
Add missing change from last commit