Refactoring

Refactored code and added comments/ideas.