Add shard management
Fix stack smashing
Add some tests