Better implementation that passes tests