Reorder code cc:@Thorsson
Fix detached form fragility