#2 refactored code, added documentation