Refactored code, closes #2