Big code clean up. Hopefully mush more efficient now