moving finalize statement to top