fixed rotation and implemented bounds checking