Code and perf improvements