refactored and added documentation