General improvements to code, fixed a couple of potential issues.