Fix #152 - Casting error. Check before casting