fix and cleanup casting and debug messages