Fixed issue #1

also addes some safety checks