More tests, clean up and otimization