Cleaned up a lot of code, making it more logical (hopefully).