Refactoring in progress.
Refactoring done, needs comments.