Various Bug Fixes

Static Analysis and cleanup of todos.