Clean up sharding-related code

Add more comments, fix style issues, etc.