moved code, made functional, removed globals