Merged Nicks code with my own, now semi-stable