Revert "Revert "Invalid pointer fix and getClass fix""

This reverts commit ab8196f20ee44b122dd12642eb00a87965d7e8e5.