use closure to avoid global vars