Massive refactor to speed things up considerably.