Fix more bugs
Minor optimizations