code tweaks for errors and performance