refactored code heavily & added a lot of documentation