Completely new implementation with more tests