whole code made more robust thanks to IntelliJ IDEA's inspection