Working code
Refactored some code