Lot of refactoring and cleaning.

Synching it up again.