fixed various issues.

Code can now run without crashing