Refactored code and added doxygen