Refactored and added more docs