Fixes #31 + refactoring