fix and cleanup casting