Refactored some code
Refactored code and added documentation