refactored existing code & added documentation