Resolved memory bugs, improved tests