Refactor, pass 2, partially functional
final patches for pass 2