optimize switch in read(), make it shorter and more DRY