Fixes #24. Refactored as discussed.