added som checks to avoid fails. thanks to an user :)