Cleanup and code refactoring.
most things working...