some refactor, wrote rotate function