Revised by @ashi009 to support rotation.