Rearrangement-driven synthesis of nitrogen heterocycles, cyclobutanes and spirocycles