Handle invalid handle & fix an issue found during code review