Working version, refactoring needed.