working code, still need cleanup