good version with speed up because of using more arrays