More code clean up, fixed tests