Refactored - got rid of unnecessary looping