Various cleanups and efficiency improvements