moved code up to top level