Working version with tests