Where an applicant, third-country national or stateless person who was detained during the asylum border procedure provided for in no longer has a right to remain and has not been allowed to remain, Member States should be able to continue the detention for the purpose of preventing entry into the territory and carrying out a return procedure, in compliance with the guarantees and conditions for detention laid down in . It should also be possible to detain an applicant, third-country national or stateless person who was not detained during such an asylum border procedure, who no longer has a right to remain and who has not been allowed to remain, if there is a risk of absconding, if he or she avoids or hampers return, or if he or she poses a risk to public policy, public security or national security. That detention should be for as short a period as possible and should not exceed the maximum duration of the return border procedure. When the illegally staying third-country national or stateless person does not return, or is not removed, within that period and the return border procedure ceases to apply, should apply. The maximum period of detention set out in that Directive should include the period of detention applied during the return border procedure.