improve retreat code