Remove dead code and try to improve performance