Fixed todos: undefined vars and unreachable code