remove global variables; Move exit in one place