Refactored implementation. Fixes #4.