Move flag checks to function, Clean up body.close()