Improved function definitions and added a rotate function.