Cleaned up and speeded up data write/read in cache