fixing NPE's and assertion failures related to cast checking