bug fix and memory reduction