JAVA code by StanislavPonomarev