added more safeguards found out by tests