corrected casting function