refactoring a bit. added a function to check whether a number is in array