Refactored the code; fixes #17