Fix issue #2 and refactor a bit of the code.