Refactors the basic code to be generally better.

Also other stuff.