Refactored the code; fixes #24