added some additional checks and warnings, and well made it work