porting code from usmnt fork. cleaning up overall.