improve checking algorithm