Working version with some unit tests