General logic cleanup, added leave flag