Refactor edge lock and rotation matrix code