Major code refactor. All code non-functional at the moment.