put cleanup code on a separate function