More bug fixing and memory leak prevention