Added memory leak fix and testing for Encoder