This is a version with complete rotation algorithms (although it is horrible, and I have already forgotten how to work it out)