refactored and tidied hdb_shard write code